<template>
  <van-overlay class="van-overlay" v-show="show">
    <div class="wrapper">
      <div class="popop">
        <span class="Img">
          <img :src="click_cur.picture" alt />
        </span>
        <div class="name">{{click_cur.name}}</div>
        <div class="molly">
          <img src="../assets/image/moliy.png" alt />
          <span class="moliy">{{click_cur.point}}</span>
        </div>
        <div class="gou">
          <button class="butt" @click="sub(click_cur)">-</button>
          <input type="text" v-model="click_cur.num" @blur="showNum(click_cur)" />
          <button class="butt" @click="add(click_cur)">+</button>
        </div>
        <button @click="addToCar(click_cur)">加入礼品车</button>
        <span class="xx" @click="hiddenShow()">x</span>
      </div>
    </div>
  </van-overlay>
</template>
<script>
import url from "../axios/apiUrl";
import { postRequest } from "../api/index";
import { getRequest } from "../api/indexGet";
export default {
  data() {
    return {
      show: false,
      num: 1,
    };
  },

  props: ["click_cur"],

  watch: {
    allGoods(val, oldVal) {},
  },
  method: {},
  methods: {
    // 关闭弹窗
    hiddenShow() {
      var that = this;
      that.show = false;
      this.$parent.changeDia();
    },
    // 加
    add(click_cur) {
      if (click_cur.num < click_cur.stock) {
        click_cur.num = parseInt(click_cur.num) + 1;
      } else {
        click_cur.num = click_cur.stock;
        this.$notify({
          message: "商品数量不能再增加了",
          type: "error",
          color: "#ad0000",
        });
      }
    },
    // 减
    sub(click_cur) {
      if (click_cur.num > 1) {
        click_cur.num = parseInt(click_cur.num) - 1;
      } else {
        click_cur.num = 1;
      }
    },
    // 失焦
    showNum(click_cur) {
      if (click_cur.num >= click_cur.stock) {
        click_cur.num = parseInt(click_cur.stock);
      } else if (click_cur.num < 1) {
        click_cur.num = parseInt(1);
      }
    },
    // 加入礼品车
    addToCar(click_cur) {
      if (click_cur.freeGiftCart == 0) {
        postRequest(url.addGiftCar, {
          goodsId: click_cur.id,
          type: click_cur.type,
          quantity: click_cur.num,
        }).then((res) => {
          click_cur.freeGiftCart = 1;
          if (res.data.code == "000000") {
            this.$toast({
              type: "success",
              success: "商品加入成功!",
            });
            that.show = false;
          } else {
            this.$toast({
              type: "fail",
              fail: "商品加入失败",
            });
          }
        });
      } else if (click_cur.freeGiftCart == 1) {
        postRequest(url.additionalGoods, {
          goodsId: click_cur.id,
          type: click_cur.type,
          quantity: click_cur.num,
        }).then((res) => {
          if (res.data.code == "000000") {
            this.$toast({
              type: "success",
              success: "商品加入成功!",
            });
          } else {
            this.$toast({
              type: "fail",
              fail: "商品加入失败",
            });
          }
        });
      }
    },
  },
};
</script>

<style lang="less" scope>
.van-overlay {
  background-color: rgba(0, 0, 0, 0.4);
  .wrapper {
    display: flex;
    align-items: center;
    justify-content: center;
    height: 100%;
    .popop {
      width: 6.8rem;
      height: 6.9rem;
      color: white;
      padding: 0.5rem 1rem;
      border-radius: 0.34rem;
      text-align: center;
      margin: 0 auto;
      background: #ffffff;
      z-index: 2000;
      .Img {
        width: 2.3rem;
        height: 2.3rem;
        box-sizing: border-box;
        img {
          width: 2.3rem;
          height: 2.3rem;
        }
      }
      .name {
        color: #343434;
        font-weight: bold;
        font-size: 0.32rem;
        margin-bottom: 0.3rem;
        margin-top: 0.3rem;
      }
      .molly {
        margin-top: 0.3rem;
        margin-bottom: 0.3rem;
        .moliy {
          color: #1fb808;
          font-size: 0.32rem;
          font-weight: bold;
        }
        img {
          font-size: 0.3rem;
          width: 0.3rem;
          height: 0.3rem;
        }
      }
      .gou {
        display: flex;
        justify-content: center;
        margin-top: 0.3rem;
        .butt {
          width: 0.68rem;
          height: 0.68rem;
          border-radius: 0.68rem;
          font-size: 0.42rem;
        }
        input {
          border: none;
          width: 1.68rem;
          height: 0.68rem;
          border-radius: 0.68rem;
          // border:0.01rem solid #2e2e2e;
          text-align: center;
          margin: 0.22rem;
          color: #000000;
        }
      }
      button {
        width: 5.6rem;
        height: 0.88rem;
        background: #1eb808;
        opacity: 0.88;
        border-radius: 0.44rem;
        border: none;
        margin-top: 0.3rem;
      }
      .xx {
        color: #222;
        font-size: 0.48rem;
        position: absolute;
        top: 0;
        right: 0;
        margin-right: 0.3rem;
        margin-top: 0.3rem;
      }
    }
  }
}
</style>